﻿
#include <iostream>
#include <iomanip>
#include "stddef.h"
#include "mylist.h"

using namespace std;

LinkList::LinkList()
{
    head = new ListNode();
    head->next = nullptr;
    head->data = NULL;
    length = 0;
}
LinkList::~LinkList()
{
    if (length == 0)
    {
        delete head;    //删除头节点空间
        head = nullptr;    
        return;
    }
    ListNode* p = head->next;
    delete head;
    while(p != nullptr)
    { 
        ListNode* tmpptr = p->next;
        delete p;
        p = tmpptr;
    }
    length = 0;
}

void LinkList::print()
{
    if (length == 0) return;
    ListNode* temp = head->next;
    cout <<"current list:"<< endl;
    int i;
    for (i = 0; i < length; i++)
    {
        cout << setw(4) <<temp->data;
        temp = temp->next;
    }
    cout << endl << endl;

    temp = nullptr;
}
ListNode* LinkList::findNode(int position)
{
    ListNode* temp = new ListNode();
    if (position > length+1)
    {
        cout<<"position"<<position<<"out of range!" << endl;
        temp->data = NULL;
        temp->next = nullptr;
        return temp;
    }
    temp = head;
    while (position--)
    {
        temp = temp->next;
    }
    return temp;
}

void LinkList::pushHead(int data)    //头插
{
    ListNode* cur = new ListNode();
    cur->data = data;
    cur->next = head->next;
    head->next = cur;
    cur = nullptr;
    return;
}

void LinkList::pushBack(int data)   //尾插
{
    ListNode* cur = new ListNode();
    cur->data = data;
    cur->next = nullptr;

    ListNode* temp = findNode(length);
    temp->next = cur;

    cur = nullptr;
    temp = nullptr;
    length++;
}

void LinkList::insert(int data, int position)//指定位置插
{
    if (position > length+1 || position < 1)
    {
        cout << "position" << position << "out of range" << endl;
        cout << endl;
        return;
    }
    if (position == length+1)
    {
        pushBack(data);
    }

    ListNode* pre = findNode(position-1);
    ListNode* cur = new ListNode();

    cur->data = data;
    cur->next = pre->next;
    pre->next = cur;
    cur = nullptr;
    pre = nullptr;

    length++;
    return;
}

void LinkList::remove(int position)
{
    if (position > length + 1 || position < 1)
    {
        cout << "position" << position << "out of range" << endl;
        cout << endl;
        return;
    }

    ListNode* pre = findNode(position - 1);
    ListNode* cur = pre->next;

    pre->next = cur->next;

    delete cur;

    pre = nullptr;
    cur = nullptr;
    length--;

}
void LinkList::reverse()
{
    if (length < 2)
    {
        return;
    }
    ListNode* pre = head->next;
    ListNode* cur = pre->next;
 
    while (cur)
    {
        ListNode* next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }

    head->next->next = nullptr;
    head->next = pre;
}


int main()
{
    LinkList Testlist;
    int i = 1;
    for (i = 1; i < 6; i++)
    {
        Testlist.pushBack(i);
    }
    Testlist.print();

    cout<<"insert:"<<endl;

    Testlist.insert(6,3);
    Testlist.print();

    Testlist.reverse();
    Testlist.print();
    
    return 0;
}

